package hotciv.view;

import minidraw.standard.*;
import minidraw.framework.*;
import java.awt.*;

import hotciv.framework.*;
import hotciv.standard.*;

/** MapView: A MiniDraw DrawingView to display the 
    Game's set of tiles.

    Responsibilities:
    1) Draw the world, consisting of a set of tiles.
<#if type == "code">

<#include "/data/author.txt">
</#if>
*/

public class MapView 
  extends StandardDrawingView {
  
  Game game;
  
  public MapView( DrawingEditor editor, Game game ) {
    super(editor, 
          new Dimension( GfxConstants.TILESIZE * Game.WORLDSIZE,
                         GfxConstants.TILESIZE * Game.WORLDSIZE ) );
    this.game = game;
  }

  /** draw the viewers background. As we redrawn everything
   * from scratch every time, all graphics that must
   * appear has to be drawn in this method.
   */
  public void drawBackground(Graphics g) {
    ImageManager im = ImageManager.getSingleton();
    Image img;
    Position p;
    Font font = new Font("Helvetica", Font.BOLD, 24);
    
    g.setFont(font);
    g.fillRect(0, 0, getBounds().width, getBounds().height);
       
    for ( int r = 0; r < Game.WORLDSIZE; r++ ) {
      for ( int c = 0; c < Game.WORLDSIZE; c++ ) {
        p = new Position(r,c);
        int xpos = GfxConstants.getXFromColumn(c);
        int ypos = GfxConstants.getYFromRow(r);
        // Draw proper terrain
        Tile t = game.getTile(p);
        String image_name = t.getTypeString();
        // special handling of ocean coastlines
        if ( image_name == GameConstants.OCEANS ) {
          image_name = image_name + 
            MapAlgorithms.getCoastlineCoding(game, p );
        }
        img = im.getImage( image_name );
        g.drawImage( img, xpos, ypos, null );
        
        // Draw cities if any
        City city = game.getCityAt(p);
        if ( city != null ) {
          img = im.getImage( "city" );
          g.setColor(city.getOwner().getColor());
          g.fillRect(xpos+1, ypos+1, 
              GfxConstants.TILESIZE-2, 
              GfxConstants.TILESIZE-2 );
      
          g.drawImage( img, xpos, ypos, null);

          g.setColor(Color.white);
          
          String size = ""+city.getSize();
          g.drawString(size, 
              xpos + GfxConstants.CITY_SIZE_OFFSET_X, 
              ypos + GfxConstants.CITY_SIZE_OFFSET_Y);
        }
      }
    }
  }
  public Dimension getPreferredSize() {
    return new Dimension( 
          GfxConstants.TILESIZE * Game.WORLDSIZE, 
          GfxConstants.TILESIZE * Game.WORLDSIZE );
  }
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
}
